//
//  ZDStatusCell.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/10/31.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZDStatusCell: UITableViewCell {
    
    weak var delegate: ZDStatusCellDelegate?
    
    //设置全局margin
    let margin: CGFloat = 8
    
    //保存pictureView的宽、高约束
    var pictureViewWidthC: NSLayoutConstraint?
    
    var pictureViewHeightC: NSLayoutConstraint?

    //微博模型
    var status: ZDStatus? {
    
        didSet {
        
//            print("父类status：\(status)")
            
            topView.status = status
            
//            contentLabel.text = status?.text
            //设置属性文本
            contentLabel.attributedText = status?.attributeText
            
            pictureView.status = status
            
//            let count = arc4random_uniform(1000) % 4
//            
//            pictureViewWidthC?.constant = CGFloat(count) * 90
//            pictureViewHeightC?.constant = CGFloat(count) * 90
            
            let pictureViewSize = pictureView.calculateSize()
            
//            print("size:\(pictureViewSize)")
            
            pictureViewWidthC?.constant = pictureViewSize.width
            pictureViewHeightC?.constant = pictureViewSize.height
            
            //设置立刻重新布局
//            pictureView.layoutIfNeeded()
        }
    }
    
    
    //MARK: - 构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 返回cell的行高 ———— cell的行高与数据模型有关
    func rowHight(status: ZDStatus?) -> CGFloat {
        
        //设置数据模型
        self.status = status
        
        //重新布局
        layoutIfNeeded()
    
        //返回bottomView的最大y
        return CGRectGetMaxY(bottomView.frame)
        
    }
   
    
    //MARK: - 添加子控件
    func prepareUI() {

        //添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        
        //布局约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.width, height: 54))
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: margin, y: margin))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: UIScreen.width - 2 * margin))
        
//        let pictureViewConstraints = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 0, height: 0), offset: CGPoint(x: 0, y: margin))
//        
//        //保存宽约束
//        pictureViewWidthC = pictureView.ff_Constraint(pictureViewConstraints, attribute: NSLayoutAttribute.Width)
//        
//        //保存高约束
//        pictureViewHeightC = pictureView.ff_Constraint(pictureViewConstraints, attribute: NSLayoutAttribute.Height)
        
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.width, height: 44), offset: CGPoint(x: -margin, y: margin))
        
        //自动计算行高 必须设置该属性 ———— 当子控件约束动态改变时，系统自动计算的行高不准确 
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
    }

    
    //MARK: - 懒加载
    private lazy var topView: ZDTopView = ZDTopView()
    
    lazy var contentLabel: FFLabel = {
    
        let label = FFLabel(fontSize: 14, color: UIColor.blackColor())
        
        //设置代理
        label.labelDelegate = self
        
        label.numberOfLines = 0
        
        return label
    }()
    
    lazy var pictureView: ZDPictureView = ZDPictureView()
    
    lazy var bottomView: ZDBottomView = ZDBottomView()
}


//实现代理方法
extension ZDStatusCell: FFLabelDelegate {

    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
//        print(text)
        
        //如果点击的是网址 就弹出对应的内容
        if text.hasPrefix("http") {
        
            delegate?.urlClick(NSURL(string: text)!)
        }
    }
}

protocol ZDStatusCellDelegate: NSObjectProtocol {
    
    func urlClick(url: NSURL)
}
